/**
 * Created by Demon on 2015/5/8.
 */
var NaturePage = function(){
    var saveValidate = function () {
        var altitude = $("#altitude").val();
        if (altitude == "") {
            toast.error("海拔高度 不能为空！")
            return false;
        }
        var annualAverageTemperature = $("#annualAverageTemperature").val();
        if (annualAverageTemperature == "") {
            toast.error("年平均气温 不能为空！")
            return false;
        }

        if ($("#localPressureState").is(':checked')) {
            var localPressure = $("#localPressure").val();
            if (localPressure == "") {
                toast.error("当地大气压KPa 不能为空！")
                return false;
            }
        }

        if ($("#localPressureOtherState").is(':checked')) {
             var localPressureOther = $("#localPressureOther").val();
             if (localPressureOther == "") {
                 toast.error("当地大气压mmHg 不能为空！")
                 return false;
             }
         }

        var outdoorCalculatedTemperature = $("#outdoorCalculatedTemperature").val();
        if (outdoorCalculatedTemperature == "") {
            toast.error("采暖室外计算温度 不能为空！")
            return false;
        }
        var summerOutdoorCalculateTemperature = $("#summerOutdoorCalculateTemperature").val();
        if (summerOutdoorCalculateTemperature == "") {
            toast.error("夏季通风室外计算温度 不能为空！")
            return false;
        }

        var annualAverageRelativeHumidity = $("#annualAverageRelativeHumidity").val();
        if (annualAverageRelativeHumidity == "") {
            toast.error("年平均相对湿度 不能为空！")
            return false;
        }

        if ($("#annualAverageAirMoistureState").is(':checked')) {
             var annualAverageAirMoisture = $("#annualAverageAirMoisture").val();
             if (annualAverageAirMoisture == "") {
                 toast.error("空气含湿量 不能为空！")
                 return false;
             }
         }

        var minDailyAverageTemperature = $("#minDailyAverageTemperature").val();
        if (minDailyAverageTemperature == "") {
            toast.error("最低日平均温度 不能为空！")
            return false;
        }

        if ($("#localDensityMoistAirState").is(':checked')) {
             var localDensityMoistAir = $("#localDensityMoistAir").val();
             if (localDensityMoistAir == "") {
                 toast.error("当地湿空气密度 不能为空！")
                 return false;
             }
         }

        return true;
    }

    var getNature = function () {
        var operateFiled =
        {
            localPressureState: $("#localPressureState").is(':checked') ? "1" : "0",
            localPressureOtherState: $("#localPressureOtherState").is(':checked') ? "1" : "0",
            annualAverageAirMoistureState: $("#annualAverageAirMoistureState").is(':checked') ? "1" : "0",
            localDensityMoistAirState: $("#localDensityMoistAirState").is(':checked') ? "1" : "0"
        }
        return {
            powerCaseId: $("#caseId").val(),
            altitude: $("#altitude").val(),
            annualAverageTemperature: $("#annualAverageTemperature").val(),
            outdoorCalculatedTemperature: $("#outdoorCalculatedTemperature").val(),
            annualAverageRelativeHumidity: $("#annualAverageRelativeHumidity").val(),
            minDailyAverageTemperature: $("#minDailyAverageTemperature").val(),
            summerOutdoorCalculateTemperature: $("#summerOutdoorCalculateTemperature").val(),
            localPressure: operateFiled.localPressureState == 1 ? $("#localPressure").val() : 0,
            localPressureOther: operateFiled.localPressureOtherState == 1 ? $("#localPressureOther").val() : 0,
            annualAverageAirMoisture: operateFiled.annualAverageAirMoistureState == 1 ? $("#annualAverageAirMoisture").val() : 0,
            localDensityMoistAir: operateFiled.annualAverageAirMoistureState == 1 ? $("#localDensityMoistAir").val() : 0,
            localPressureState: operateFiled.localPressureState,
            localPressureOtherState: operateFiled.localPressureOtherState,
            annualAverageAirMoistureState: operateFiled.annualAverageAirMoistureState,
            localDensityMoistAirState: operateFiled.localDensityMoistAirState
        }
    }
    return{
      init:function(){
          $("#getNa").on('click',function() {
             $("#tbody tr").remove();
             var cityName = $("#cityName").val().trim();
              $.ajax({
                  url:basePath+"nature/getHelp/cityName",
                  dataType:"json",
                  type:"post",
                  data:"cityName="+ cityName,
                  success:function(data){
                      $("#tbody").append(
                          "<tr>" +
                              "<td>" +data.cityName + "</td>" +
                              "<td>" +data.altitude + "</td>" +
                              "<td>" +data.hpaSummer + "</td>" +
                              "<td>" +data.hpaWinter +"</td>" +
                              "<td>" +data.avgTep + "</td>" +
                              "<td>" +data.outCulTep + "</td>" +
                              "<td>" +data.summerOutTep + "</td>" +
                              "<td>" +data.dayLowTep+ "</td>" +
                              "<td>" +data.relHum +"</td>" +
                              "</tr>"
                      );
                  }
              })
          });

          $("#getNaAll").on('click',function(){
              $("#tbody tr").remove();
              $.get(basePath + "nature/getNaAll",function(data){
                  for(var i=0;i<=data.length;i++){
                     $("#tbody").append(
                         "<tr>" +
                             "<td>" +data[i].cityName + "</td>" +
                             "<td>" +data[i].altitude + "</td>" +
                             "<td>" +data[i].hpaSummer + "</td>" +
                             "<td>" +data[i].hpaWinter +"</td>" +
                             "<td>" +data[i].avgTep + "</td>" +
                             "<td>" +data[i].outCulTep + "</td>" +
                             "<td>" +data[i].summerOutTep + "</td>" +
                             "<td>" +data[i].dayLowTep+ "</td>" +
                             "<td>" +data[i].relHum +"</td>" +
                          "</tr>"
                      );
                 }
              });
          });

          $("#localPressureState").bind("click", function () {
              NaturePage.localPressureStateClick($(this))
          })


          $("#localPressureOtherState").bind("click", function () {
              NaturePage.localPressureOtherStateClick($(this))
          })

          $("#annualAverageAirMoistureState").bind("click", function () {
              NaturePage.annualAverageAirMoistureStateClick($(this))
          })

          $("#localDensityMoistAirState").bind("click", function () {
              NaturePage.localDensityMoistAirStateClick($(this))
          })

       },
        localPressureStateClick: function (obj,state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("#localPressure").attr("readonly", false)
            } else {
                if (state == undefined) {
                    $("#localPressure").val("")
                }
                $("#localPressure").attr("readonly", true);
            }
        },
        localPressureOtherStateClick: function (obj,state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("#localPressureOther").attr("readonly", false)
            } else {
                if (state == undefined) {
                    $("#localPressureOther").val("")
                }
                $("#localPressureOther").attr("readonly", true);
            }
        },
        annualAverageAirMoistureStateClick: function (obj,state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("#annualAverageAirMoisture").attr("readonly", false)
                $("#annualAverageAirMoistureLabel").addClass("aTypeStyle");
            } else {
                if (state == undefined) {
                    $("#annualAverageAirMoisture").val("")
                }
                $("#annualAverageAirMoisture").attr("readonly", true);
                $("#annualAverageAirMoistureLabel").removeClass("aTypeStyle");
            }
        },
        localDensityMoistAirStateClick: function (obj,state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("#localDensityMoistAir").attr("readonly", false)
                $("#localDensityMoistAirLabel").addClass("aTypeStyle");
            } else {
                if (state == undefined) {
                    $("#localDensityMoistAir").val("")
                }
                $("#localDensityMoistAir").attr("readonly", true);
                $("#localDensityMoistAirLabel").removeClass("aTypeStyle");
            }
        },

        natureSave: function () {
            if (!saveValidate())return;
            var addFlag = $("#addFlag").val();
            if (addFlag == "add" || (addFlag != "add" && confirm("确定重新计算并保存吗？"))) {
                Metronic.blockUI({
                    boxed: true,
                    message: "数据加载中..."
                });
                $.ajax({
                    url: basePath + "nature/natureSave",
                    type: "post",
                    dataType: "json",
                    data: getNature(),
                    success: function (data) {
                        Metronic.unblockUI();
                        if (data.ok) {
                            formSub(basePath + "nature?menuId=14&message=success")
                        } else if (data.error) {
                            toast.error(data.returnMsg);
                            return false;
                        }

                    }
                })
            }
        },
        natureNext: function () {
            var addFlag=$("#addFlag").val();
            if (addFlag=="add") {
                toast.error("请先填写本页数据，保存成功后再进行下一页操作！");
                return;
            }
            formSub(basePath + "boiler/gasogene?menuId=18")
        }
    }
}();
